/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2015-5-13         yellowlgx
 */

package com.skyworth.ui.api;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.FrameLayout;

import com.skyworth.util.SkyScreenParams;

/**
 * @author yellowlgx
 * @version TODO (write something)
 * @ClassName SkyWithBGLoadingView * @Description
 * <p>
 * 带背景的loadingView，也就是普通的View，直接创建就能使用，大小由使用者控制，其中显示时调用showLoading()，消失调用
 * dismissLoading()即可；
 * </p>
 * @date 2015-5-13
 */
public class SkyWithBGLoadingView extends FrameLayout {
    private NewSkyLoadingView loadingView;
    private LayoutParams mLoadP;

    public SkyWithBGLoadingView(Context context) {
        this(context, null);
    }

    public SkyWithBGLoadingView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SkyWithBGLoadingView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mLoadP = new LayoutParams(SkyScreenParams.getInstence(context).getResolutionValue(330),
                SkyScreenParams.getInstence(context).getResolutionValue(30), Gravity.CENTER);
        loadingView = new NewSkyLoadingView(context);
        this.addView(loadingView, mLoadP);
        super.setVisibility(View.INVISIBLE);
    }

    public void setScaleW_H(int w, int h) {
//        LinearLayout.LayoutParams loadP = (LinearLayout.LayoutParams) loadingView.getLayoutParams();
//        loadP.width = (int) ((float) (w * loadP.width) / mLoadP.width + 0.5f);
//        loadP.height = (int) ((float) (h * loadP.height) / mLoadP.height + 0.5f);
//        loadingView.setLayoutParams(loadP);
//
//        mLoadP.width = (int) ((float) (w * mLoadP.width) / mLoadP.width + 0.5f);
//        mLoadP.height = (int) ((float) (h * mLoadP.height) / mLoadP.height + 0.5f);
//        bgLayout.setLayoutParams(mLoadP);
//
//        ViewGroup.LayoutParams thisP = this.getLayoutParams();
//        thisP.width = w;
//        thisP.height = h;
//        this.setLayoutParams(thisP);
    }

    public int getLoadingViewWidth() {
        return mLoadP.width;
    }

    public int getLoadingViewHeight() {
        return mLoadP.height;
    }

    public void destroy()
    {
        loadingView.destroy();
    }

    private static final int MSG_LOADING_SHOW = 1;
    private static final int MSG_LOADING_HIDE = 2;
    private final Handler handler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(Message msg) {
            Log.d("LOADING", "handleMessage:" + msg.what);
            switch (msg.what) {
                case MSG_LOADING_SHOW: {
                    SkyWithBGLoadingView.super.setVisibility(View.VISIBLE);
                    loadingView.startAnim();
                    break;
                }
                case MSG_LOADING_HIDE: {
                    loadingView.stopAnim();
                    SkyWithBGLoadingView.super.setVisibility(View.GONE);
                    break;
                }
            }
        }
    };

    @Override
    public void setVisibility(int visibility) {
        //乱调  setVisibility
//        StackTraceElement[] elements = Thread.currentThread().getStackTrace();
//        for (StackTraceElement element : elements) {
//            Log.e("LOADING", element.toString());
//        }
        Log.e("LOADING", "stupid call setVisibility " + visibility);
    }

    public void showLoading(long delay) {
        Log.d("LOADING", "call showLoading delay:" + delay);
        synchronized (handler) {
            boolean bSpinning = isSpinning();
            boolean flag = false;
            if (handler.hasMessages(MSG_LOADING_HIDE)) {
                Log.d("LOADING", "call showLoading hasHide continue show!");
                handler.removeMessages(MSG_LOADING_HIDE);
                if (!bSpinning)
                    flag = true;
            } else {
                flag = true;
            }
            if (flag) {
                boolean hasShow = handler.hasMessages(MSG_LOADING_SHOW);
                Log.d("LOADING", "call showLoading bSpinning:" + bSpinning + "  hasShow:" + hasShow);
                if (!hasShow)
                    handler.sendEmptyMessageDelayed(MSG_LOADING_SHOW, delay);
            }
        }
    }

    public void showLoadingWithDelay() {
        showLoading(1500);
    }

    public void showLoading() {
        showLoading(0);
//        SkyWithBGLoadingView.this.setVisibility(View.VISIBLE);
//        loadingView.startAnim();
    }

    // 设置圆圈宽度
    public void setStrokeWidth(int width) {

    }

    public void dismissLoading() {
        Log.d("LOADING", "call dismissLoading");
        synchronized (handler) {
            handler.removeMessages(MSG_LOADING_SHOW);
            boolean hasHide = handler.hasMessages(MSG_LOADING_HIDE);
            Log.d("LOADING", "call dismissLoading hasHide:" + hasHide);
            if (!hasHide)
                handler.sendEmptyMessageDelayed(MSG_LOADING_HIDE, 200);
        }
//        loadingView.stopAnim();
//        SkyWithBGLoadingView.this.setVisibility(View.GONE);
    }

    public boolean isSpinning() {
        return loadingView.isAnimRunning();
    }

}
